<?php

namespace App\Controllers;

class Travel extends BaseController {

    public function index() {
        // connect to the model
        $places = new \App\Models\Places();
        // retrieve all the records
        $records = $places->findAll();

        $table = new \CodeIgniter\View\Table();
        $headings = $places->fields;
        $displayHeadings = array_slice($headings, 1, 2);
        $table->setHeading(array_map('ucfirst', $displayHeadings));

        foreach ($records as $record) {
            $nameLink = anchor("travel/showme/$record->id", $record->name);
            $table->addRow($nameLink, $record->Native);
        }

        $template = [
            'table_open' => '<table cellpadding="5px">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];

        $fields = [
            'title' => 'China chef',
            'heading' => 'China chef',
            'footer' => 'Copyright Xavier'
        ];

        $table->setTemplate($template);

        // get a template parser
        $parser = \Config\Services::parser();

        return $parser->setData($fields)
                        ->render('templates\top') .
                $table->generate() .
                        $parser->setData($fields)
                        ->render('templates\bottom');
    }

    public function showme($id) {
        // connect to the model
        $places = new \App\Models\Places();
        // retrieve all the records
        $record = $places->find($id);

        $table = new \CodeIgniter\View\Table();
        $headings = $places->fields;
        $table->addRow(anchor("travel/edit/" . $record['id'], "Edit"));
        $table->addRow(ucfirst($headings[0]), $record['id']);
        $table->addRow(ucfirst($headings[1]), $record['native']);
        $table->addRow(ucfirst($headings[2]), $record['Ethnic']);
        $table->addRow(ucfirst($headings[3]), $record['birth']);
        $table->addRow(ucfirst($headings[4]), $record['representative']);
        $table->addRow(ucfirst($headings[5]), $record['height']);
        $table->addRow(ucfirst($headings[6]), $record['Gender']);
        $table->addRow(ucfirst($headings[7]), $record['position']);
        $table->addRow(['data' => $imgString, 'colspan' => 2]);

        // get a template parser
        $parser = \Config\Services::parser();
        $fields = [
            'title' => 'China chef',
            'heading' => $record['name'],
            'footer' => 'Copyright Xavier'
        ];

        // tell it about the substitions
        return $parser->setData($fields)
                        ->render('templates\top') .
                $table->generate() .
                        $parser->setData($fields)
                        ->render('templates\bottom');
    }

    public function edit($id = NULL) {
        $places = new \App\Models\Places();
        $record = $places->find($id);
        $fields = [
            'title' => 'Edit a China chef',
            'heading' => 'Edit a China chef',
            'footer' => 'Copyright Xavier'
        ];
        $parser = \Config\Services::parser();
        return $parser->setData($fields)
                        ->render('templates\top') .
                        $parser->setData($record)
                        ->render('editplace') .
                        $parser->setData($fields)
                        ->render('templates\bottom');
    }

    public function handle() {
        $places = new \App\Models\Places();
        helper(['form', 'url']);
        $record = $places->find($this->request->getPost('id'));
        if ($this->validate($places->validationRules)) {
            $places->save([
                'id' => $this->request->getPost('id'),
                'name' => $this->request->getPost('name'),
                'Ethnic' => $this->request->getPost('Ethnic'),
                'native' => $this->request->getPost('native'),
                'birth' => $this->request->getPost('birth'),
                'representative' => $this->request->getPost('representative'),
                'height' => $this->request->getPost('height'),
                'Gender' => $this->request->getPost('Gender'),
                'professional' => $this->request->getPost('professional'),
            ]);
            $fields = [
                'title' => 'Edit a China chef',
                'heading' => 'Edit a China chef',
                'footer' => 'Copyright Xavier'
            ];
            $parser = \Config\Services::parser();
            //Top of page
            $returnView = $parser->setData($fields)->render('templates\top');

            //Body of page
            if (!$this->validate($places->validationRules)) {
                $returnView .= $parser->renderString($this->validator->listErrors());
                $returnView .= $parser->setData($this->request->getPost())
                        ->render('editplace');
            } else {
                $returnView .= $parser->renderString("<p>Success!</p>");
                $returnView .= $parser->setData($record)
                        ->render('editplace');
            }

            //Bottom of page
            $returnView .= $parser->setData($fields)
                    ->render('templates\bottom');
            return $returnView;
        }
    }

}
